#!/usr/bin/env python3
import os
import re
import sys

cur_dir = os.getcwd() + os.sep
rootDir = cur_dir[:cur_dir.find("DownPython\\") + len("DownPython\\")]  # 获取myProject，也就是项目的根路径

sys.path.append(rootDir)
import porn

from common import LoggerTool

logger = LoggerTool.get_logger(__name__)


def get_jh_done(file_dir, file_type):
    result = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            splitext = os.path.splitext(file)
            if splitext[1] == ('.' + file_type) and (cur_year in splitext[0] or pre_year in splitext[0]):
                result.append(os.path.join(root, file))

    return result


def write_common(start_page, end_page):
    furl_tool = porn.FurlTool(down_url)
    file_name_list = furl_tool.get_down_map()
    if file_name_list is None and len(file_name_list) == 0:
        logger.error('找不到已下载文件，无法读取数据。。。。。。')
        return
    isdigit = furl_tool.is_digital('filter')
    read_lines = []
    cur_year_done_file = ''
    pre_year_done_file = ''
    orgin_done_file_path = ''

    # 只读取上个月和这个月的连接
    for item, values in file_name_list.items():
        if not bool(re.search(r'\d', item)):
            orgin_done_file_path = values[0]
        if item.find(cur_year) > 0:
            cur_year_done_file = values[0]
        if item.find(pre_year) > 0:
            pre_year_done_file = values[0]
    # 判断当前月文件是否存在，如果不存在，用上个月文件生成
    if not cur_year_done_file.strip() and not pre_year_done_file.strip():
        logger.error('cur_month_done_file and pre_month_done_file 不能同时为空 ')
    if not cur_year_done_file.strip():
        logger.warning('cur_month_done_file  is null')
        cur_year_done_file = orgin_done_file_path.split('.text')[0] + '-' + cur_year + '.text'
    all_dir = '../'
    # 获取所有已下载
    all_done = get_jh_done(all_dir, 'text')
    for item in all_done:
        with open(item) as jh_file:
            read_lines.extend(jh_file.read().splitlines())

    if isdigit:
        logger.info('保存非jh 链接')
        porn.write_non_jh(down_url, start_page, end_page, read_lines, cur_year_done_file)
    else:
        not_jh = '../../'
        logger.info('保存 jh 链接')
        porn.write_jh_thread(down_url, start_page, end_page, read_lines, cur_year_done_file)


def write_common_2025(start_page, end_page):
    furl_tool = porn.FurlTool(down_url)

    isdigit = furl_tool.is_digital('filter')
    read_lines = []
    cur_year_done_file = '../jh/zpdr_ycsq_jh/Done-JH-ZPDR-2025-03.text'
    pre_year_done_file = ''
    orgin_done_file_path = ''

    all_dir = '../'

    if isdigit:
        logger.info('保存非jh 链接')
        porn.write_non_jh(down_url, start_page, end_page, read_lines, cur_year_done_file)
    else:
        not_jh = '../../'
        logger.info('保存 jh 链接')
        porn.write_jh_thread(down_url, start_page, end_page, read_lines, cur_year_done_file)


if __name__ == '__main__':
    date_tool = porn.DateTool()
    cur_month = date_tool.cur_month
    cur_year = '2025'
    pre_month = date_tool.pre_month
    pre_year = '2024'
    print(cur_year)
    print(pre_year)
    JH_LIST=porn.JH_ZPDR
    for index in range(0, len(JH_LIST)):
        down_url = JH_LIST[index]
        print(down_url)
        write_common(1, 3)
